package com.example.util;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class GetDateUtils {
    public static String getCurrentDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        //现在日期
        String now = format.format(new Date());
        System.out.println("现在日期  ："+now);

        return now;
    }

    public static String getBeforeWeekDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        //过去七天
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, - 7);
        Date d = calendar.getTime();
        String day = format.format(d);
        System.out.println("过去七天  ："+day);

        return day;
    }

    public static String getBeforeMonthDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        //过去一月
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -1);
        Date m = calendar.getTime();
        String mon = format.format(m);
        System.out.println("过去一个月："+mon);

        return mon;
    }


    public static String getDate2String(String format, Date date) {
        if (date != null) {
            if (StringUtils.isEmpty(format)) {
                format = "yyyy-MM-dd";
            }

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

            return simpleDateFormat.format(date);
        } else {
            return "";
        }
    }


}
